package 代理模式.JDK代理;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class GameProxy {
    private Player player;

    public void setPlayer(Player player) {
        this.player = player;
    }

    public GameProxy(Player player) {
        this.player = player;
    }

    // 返回方法被扩充的对象，但是对象还是这里给的player
    // 只能返回Object ！
    public Object JDKDaiLi() {
        return Proxy.newProxyInstance(player.getClass().getClassLoader(),
                player.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("JDK代理开始");
                        Object res = method.invoke(player, args);
                        System.out.println("JDK代理结束");
                        return res;
                    }
                });
    }
}
